package com.sunny.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.concurrent.CountDownLatch;

public class AioEchoServer {
    static int PORT = 8080;
    static int BUFFER_SIZE = 1024;
    static String CHARSET = "utf-8"; //默认编码
    static CharsetDecoder decoder = Charset.forName(CHARSET).newDecoder(); //解码
    private CountDownLatch countDownLatch = new CountDownLatch(1);

    int port;
    //ByteBuffer buffer;
    AsynchronousServerSocketChannel serverChannel;

    public AioEchoServer(int port) throws IOException {
        this.port = port;
        //this.buffer = ByteBuffer.allocate(BUFFER_SIZE);
        this.decoder = Charset.forName(CHARSET).newDecoder();
    }

    private void listen() throws Exception {

        //打开一个服务通道
        //绑定服务端口
        this.serverChannel = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port), 100);
        this.serverChannel.accept(this, new AcceptHandler());

        countDownLatch.await();
    }


    /**
     * accept到一个请求时的回调
     */
    private class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel, AioEchoServer> {
        @Override
        public void completed(final AsynchronousSocketChannel client, AioEchoServer attachment) {
            try {
                System.out.println("远程地址：" + client.getRemoteAddress());
                //tcp各项参数
                client.setOption(StandardSocketOptions.TCP_NODELAY, true);
                client.setOption(StandardSocketOptions.SO_SNDBUF, 1024);
                client.setOption(StandardSocketOptions.SO_RCVBUF, 1024);

                if (client.isOpen()) {
                    System.out.println("client.isOpen：" + client.getRemoteAddress());
                    final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
                    buffer.clear();
                    client.read(buffer, client, new ReadHandler(buffer));
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                attachment.serverChannel.accept(attachment, this);// 监听新的请求，递归调用。
            }
        }

        @Override
        public void failed(Throwable exc, AioEchoServer attachment) {
            try {
                exc.printStackTrace();
            } finally {
                attachment.serverChannel.accept(attachment, this);// 监听新的请求，递归调用。
            }
        }
    }

    /**
     * Read到请求数据的回调
     */
    private class ReadHandler implements CompletionHandler<Integer, AsynchronousSocketChannel> {

        private ByteBuffer buffer;
        private int cnt = 0;
        private StringBuilder sb = new StringBuilder();

        public ReadHandler(ByteBuffer buffer) {
            this.buffer = buffer;
        }

        @Override
        public void completed(Integer result, AsynchronousSocketChannel attachment) {
            try {
                if (result < 0) {// 客户端关闭了连接
                    AioEchoServer.close(attachment);
                } else if (result == 0) {
                    System.out.println("空数据"); // 处理空数据
                } else {
                    cnt += result;
                    System.out.println(cnt);
//                      System.out.println(charBuffer.toString()); //接收请求
                    if(cnt >= 10){
                        System.out.println("写回请求");
//                        System.out.println(data);
                        // 读取请求，处理客户端发送的数据
//                        buffer.flip();
//                    CharBuffer charBuffer = AioEchoServer.decoder.decode(buffer);
//                    System.out.println(charBuffer.toString()); //接收请求
//
//                    //响应操作，服务器响应结果
//                    buffer.clear();
//                    String res = "HTTP/1.1 200 OK" + "\r\n\r\n" + "hellworld";
//                    buffer = ByteBuffer.wrap(res.getBytes());
                        buffer.flip();
                        attachment.write(buffer, attachment, new WriteHandler(buffer));//Response：响应。
                    }else{
                        System.out.println("继续获取");
//                        buffer.flip();
                        attachment.read(buffer, attachment, this);
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failed(Throwable exc, AsynchronousSocketChannel attachment) {
            exc.printStackTrace();
            AioEchoServer.close(attachment);
        }
    }

    /**
     * Write响应完请求的回调
     */
    private class WriteHandler implements CompletionHandler<Integer, AsynchronousSocketChannel> {
        private ByteBuffer buffer;

        public WriteHandler(ByteBuffer buffer) {
            this.buffer = buffer;
        }

        @Override
        public void completed(Integer result, AsynchronousSocketChannel attachment) {
//            buffer.clear();
//            AioEchoServer.close(attachment);
            buffer.compact();
            attachment.read(buffer, attachment, new ReadHandler(buffer));
        }

        @Override
        public void failed(Throwable exc, AsynchronousSocketChannel attachment) {
            exc.printStackTrace();
            AioEchoServer.close(attachment);
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("正在启动服务...");
            AioEchoServer server = new AioEchoServer(PORT);
            server.listen();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void shutdown(){
        try {
            serverChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        countDownLatch.countDown();
    }

    private static void close(AsynchronousSocketChannel client) {
        try {
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}